/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.dialogs;

import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;
import org.jfonia.actions.ActionFactory;
import org.jfonia.actions.CreateChordPartAction;
import org.jfonia.actions.CreateLyricsPartAction;
import org.jfonia.constants.ViewConstants;
import org.jfonia.images.ImageConstants;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;
import org.jfonia.view.panels.GradientPaintedBorderPanel;
import org.jfonia.view.components.UnifiedToolbarButtonUI;

/**
 *
 * @author Rik Bauwens
 */
public class NewPartDialog extends JDialog implements ActionListener
{
        private static NewPartDialog instance;
        private Component parent;

        private NewPartDialog()
        {
                setVisible(false);
                setTitle(LanguageResource.getInstance().getLabel(LabelConstants.CREATE_PART));
                setLayout(new BoxLayout(getContentPane(), BoxLayout.LINE_AXIS));
                GradientPaintedBorderPanel dialogPanel = 
                        new GradientPaintedBorderPanel(ViewConstants.DIALOG_BACKGROUND_COLOR);
                dialogPanel.add(Box.createHorizontalStrut(ViewConstants.DIALOG_LEFT_MARGIN));

                ActionFactory actionFactory = ActionFactory.getInstance();
                JPanel contentPanel = new JPanel();
                contentPanel.setLayout(new BoxLayout(contentPanel, BoxLayout.PAGE_AXIS));
                contentPanel.setOpaque(false);
                JLabel headerLabel = new JLabel(LanguageResource.getInstance().getLabel(LabelConstants.SELECT_PART_TYPE));
                headerLabel.setFont(headerLabel.getFont().deriveFont(ViewConstants.DIALOG_HEADER_FONT_SIZE));
                contentPanel.add(Box.createVerticalStrut(ViewConstants.DIALOG_TOP_MARGIN));
                contentPanel.add(headerLabel);
                contentPanel.add(Box.createVerticalStrut(ViewConstants.DIALOG_HEADER_MARGIN));
                contentPanel.add(createButton(actionFactory.getAction(ActionFactory.CREATESTAFFPARTACTION)));
                contentPanel.add(createButton(actionFactory.getAction(ActionFactory.CREATECHORDPARTACTION)));
                contentPanel.add(createButton(actionFactory.getAction(ActionFactory.CREATELYRICPARTSACTION)));
                contentPanel.add(Box.createVerticalStrut(ViewConstants.DIALOG_BOTTOM_MARGIN));
                
                dialogPanel.add(contentPanel);
                dialogPanel.add(Box.createHorizontalStrut(ViewConstants.DIALOG_RIGHT_MARGIN));
                setContentPane(dialogPanel);
                setUndecorated(true);
                setAlwaysOnTop(true);
                setModal(true);
                pack();
        }

        public static synchronized NewPartDialog getInstance()
        {
                if(instance == null)
                        instance = new NewPartDialog();
                return instance;
        }

        public void setParent(Component parent)
        {
                this.parent = parent;
                setLocationRelativeTo(parent);
        }

        @Override
        public void setVisible(boolean b)
        {
                setLocationRelativeTo(parent);
                super.setVisible(b);
        }

        private JButton createButton(AbstractAction action)
        {
                JButton button = new JButton(action);
                button.setOpaque(false);
                button.setUI(new UnifiedToolbarButtonUI());
                button.setHorizontalTextPosition(SwingConstants.RIGHT);
                button.setVerticalTextPosition(SwingConstants.CENTER);
                button.setIconTextGap(ViewConstants.DIALOG_ICON_TEXT_GAP);
                button.addActionListener(this);
                return button;
        }

        public void actionPerformed(ActionEvent e)
        {
                setVisible(false);
        }
}
